﻿using RuntimeGizmos;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EditorToolBar : MonoBehaviour {

    public UToggleGroup toggleGroup;
    UToggle[] toggles;
    public TransformGizmo mGizmos;
    void Awake()
    {
        toggles = this.transform.GetComponentsInChildren<UToggle>(true);

        toggleGroup.OnValueChanged+= OnValueChangedHandler;

    }

    private void OnValueChangedHandler(UToggle obj)
    {
        SetEditToolByName(obj.name);
        switch (obj.name)
        {
            case "move":
                mGizmos.transformType = TransformType.Move;
                break;
            case "rotate":
                mGizmos.transformType = TransformType.Rotate;
                break;
            case "scale":
                mGizmos.transformType = TransformType.Scale;
                break;
        }
 
    }

    // Use this for initialization
    void Start () {
     
    }
    public string lastEditIcon = "pan";
    public void SetEditToolByName(string name)
    {
        
        foreach (var item in toggles)
        {

            if (item.name.Equals(name))
            {
                item.isOn = true;
            }
            else
            {
                item.isOn = false;
            }
        }
       
    }
   
   // Update is called once per frame
   void Update () {

        if (Input.GetKeyDown(KeyCode.Q))
        {
            SetEditToolByName("pan");
            lastEditIcon = "pan";
        }
        if (Input.GetKeyDown(KeyCode.W))
        {
            SetEditToolByName("move");
            lastEditIcon = "move";
        }
        if (Input.GetKeyDown(KeyCode.E))
        {
            SetEditToolByName("rotate");
            lastEditIcon = "rotate";
          
        }
        if (Input.GetKeyDown(KeyCode.R))
        {
            SetEditToolByName("scale");
            lastEditIcon = "scale";
           
        }

        if (Input.GetMouseButton(2))
        {
            SetEditToolByName("pan");
        }

        if (Input.GetMouseButtonUp(2))
        {
            SetEditToolByName(lastEditIcon);
        }
    }
}
